package aom.ju.ss.view.cut

import android.os.Handler
import android.os.Looper
import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.CutListItemBinding
import aom.ju.ss.databinding.CutPlaceOrderItemBinding
import aom.ju.ss.databinding.HelpCutActivityBinding
import aom.ju.ss.lpInterface.MultiLayoutBindViewInterface
import aom.ju.ss.mode.CutOrderInfo
import aom.ju.ss.mode.CutOrderInfoData
import aom.ju.ss.mode.HelpCutList
import aom.ju.ss.mode.PAdapter2Mode
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.TimeUtil
import aom.ju.ss.utils.data
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.ListHolder
import aom.ju.ss.view.base.PAdapter2
import aom.ju.ss.view.dialog.CutDialog
import com.google.gson.Gson

/**
 * 砍价详情
 */
class HelpCutDetailActivity : BaseActivity() {

    private lateinit var binding: HelpCutActivityBinding
    lateinit var pAdapter2: PAdapter2
    private var list = arrayListOf<PAdapter2Mode>()
    override fun init() {
        binding = viewBinding as HelpCutActivityBinding

        OKHttpUtils(activity)
            .SetApiUrl(LP_API.cut_order_info)
            .SetKey("id")
            .SetValue(data)
            .POST(this)
        pAdapter2 = PAdapter2(
            list,
            { b, position, type, data ->
                if (type == 1) {
                    val ib = b.binding as CutListItemBinding
                    ib.info = data as HelpCutList
                    ib.p = mode.data.goods_info.cut_money
                }

            }, R.layout.cut_list_head, R.layout.cut_list_item
        )
        binding.rec.layoutManager = LinearLayoutManager(activity)
        binding.rec.adapter = pAdapter2

    }

    val han = Handler(Looper.getMainLooper(), Handler.Callback {
        if (it.what == 1999) {
            activity?.isFinishing?.let { isFinishing ->
                if (!isFinishing) {
                    mode.data.let { mode ->
                        val ifo = TimeUtil.sysj2(mode.goods_info.end_time)
                        binding.daojishi = ifo
                        if (ifo.js == 0) {
                            send()
                        }
                    }
                }
            }

        }
        return@Callback false
    })

    private fun send() {
        han.sendEmptyMessageDelayed(1999, 1000)
    }

    lateinit var mode: CutOrderInfo
    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        mode = Gson().fromJson(jsonString, CutOrderInfo::class.java)
        binding.info = mode.data
        binding.progressBar2.setProgress(mode.data.goods_info.p)
        binding.cut.setOnClickListener {
            if (mode.data.help_cut == 1) {
                return@setOnClickListener
            }
            CutDialog(activity!!, data, mode.data.goods_info.cut_money)
        }
        list.clear()
        list.add(PAdapter2Mode(0, ""))
        mode.data.help_cut_list.let {
            for (a in it) {
                list.add(PAdapter2Mode(1, a))
            }

        }
        pAdapter2.notifyDataSetChanged()
        send()
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        e.show
    }

    override val lay: Int = R.layout.help_cut_activity
}

class DaoJiShi(
    var tian: Int = 0,
    var shi: Int = 0,
    var fen: Int = 0,
    var miao: Int = 0,

    var js: Int = 0,
)